package com.clei.utils;

import java.io.File;
import java.io.FileInputStream;
import java.util.Scanner;

/**
 * 系统工具类
 *
 * @author KIyA
 */
public class SystemUtil {

    /**
     * scanner
     */
    private static final Scanner INPUT = new Scanner(System.in);

    /**
     * 控制台暂停 直到输入任意字符
     */
    public static void pause() {
        INPUT.next();
    }

    /**
     * 控制台暂停 直到输入指定字符串
     *
     * @param str
     */
    public static void pause(String str) {
        String s;
        while (!str.equals(s = INPUT.nextLine())) {
            PrintUtil.log(s);
        }
    }

    /**
     * 输入
     *
     * @return Scanner
     */
    public static Scanner input() {
        File inputFile = getInputFile();
        return null == inputFile ? INPUT : fileInput(inputFile);
    }

    /**
     * 输入
     *
     * @param console true/false
     * @return Scanner
     */
    public static Scanner input(boolean console) {
        return console ? consoleInput() : fileInput();
    }

    /**
     * 输入
     *
     * @return Scanner
     */
    public static Scanner consoleInput() {
        return INPUT;
    }

    /**
     * 输入
     *
     * @return Scanner
     */
    public static Scanner fileInput() {
        return fileInput(getInputFile());
    }

    /**
     * 输入
     *
     * @param file inputFile
     * @return Scanner
     */
    public static Scanner fileInput(File file) {
        if (null == file) {
            PrintUtil.log("empty file ");
            return INPUT;
        }
        try {
            FileInputStream fis = new FileInputStream(file);
            return new Scanner(fis);
        } catch (Exception e) {
            PrintUtil.log("fileInput error filePath : {}", file, e);
        }
        return INPUT;
    }

    /**
     * isWindows
     *
     * @return true/false
     */
    public static boolean isWindows() {
        String osName = System.getProperty("os.name");
        return osName.toLowerCase().contains("windows");
    }

    /**
     * inputFile
     *
     * @return File
     */
    private static File getInputFile() {
        String filePath = System.getProperty("user.dir") + File.separator + "input.txt";
        File inputFile = new File(filePath);
        return inputFile.exists() ? inputFile : null;
    }
}